/*
 * jQuery AT Window 1.0
 *
 * Copyright 2011, Marcin Baniowski
 * 
 *	Depends:
 *	jquery.ui.core.js
 *	jquery.ui.mouse.js
 *	jquery.ui.widget.js
 *	jquery.ui.draggable.js
 *	jquery.ui.resizable.js
 */

(function($) {
	$.widget("at.atwindow", {
		options: {
			autoOpen: false,
			openSpeed: 700,
			xPosition: 'left',
			yPosition: 'top',
			xAdd: 0,
			yAdd: 0,
			startWidth: 400,
			startHeight: 300,
			resizable: true,
			draggable: true,
			collapsible: true,
			close: true,
			url: null
		},

		_create: function() {
			// nadawanie numeru instancji - do unikalnych klas przy alsoResize
			if($.at.atwindow.mbcount == undefined)
				$.at.atwindow.mbcount = [];
			$.at.atwindow.mbcount.push('1');
			var instanceNumber = $.at.atwindow.mbcount.length;
			var el = this.element.hide();
			var el_content = el.text();
			var el_title = el.attr('title');
			var wrapper = this.wrapper = $('<div />').addClass('atwindow-wrapper');
			this.top = $('<div />')
				.addClass('atwindow-top')
				.appendTo(this.wrapper);
			if(this.options.draggable == true)
				this.top.css('cursor','move');
			this.topcontent = $('<div />')
				.addClass('atwindow-topcontent')
				.text(el_title)
				.appendTo(this.top);
			if(this.options.collapsible == true){
				this.min = $('<img />')
					.attr('src','images/window_min.jpg')
					.addClass('atwindow-min')
					.appendTo(this.top);
				this.max = $('<img />')
					.attr('src','images/window_max.jpg')
					.addClass('atwindow-max')
					.appendTo(this.top);
			}
			if(this.options.close == true){
			this.close = $('<img />')
				.attr('src','images/window_close.jpg')
				.addClass('atwindow-close')
				.appendTo(this.top);
			}
			this.bottom = $('<div />')
				.css({'height':this.options.startHeight-30+'px'})
				.addClass('atwindow-bottom')
				.addClass('mb-draggable-'+instanceNumber)
				.appendTo(this.wrapper);
			this.bottomcontent = $('<div />')
				.css({'height':this.options.startHeight-30+'px'})
				.addClass('atwindow-bottomcontent')
				.addClass('mb-draggable-'+instanceNumber)
				.appendTo(this.bottom);
			this.content = $('<div />')
				.addClass('atwindow-content')
				.addClass('mb-draggable-'+instanceNumber)
				.css({'width':this.options.startWidth-25-16+'px','height':this.options.startHeight-45-16+'px'}).text(el_content).appendTo(this.wrapper);
			if(this.options.resizable == true){
				this.resize = $('<img />')
					.attr('src','images/window_resize.gif')
					.addClass('atwindow-resize ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se') // TODO - pousuwać większość klas UI
					.appendTo(this.wrapper);
			}
//			this.wrapper.css('z-Index',instanceNumber);
			this.wrapper.insertAfter(el);
			this.wrapper.css({'left':this._getPosition('x')+'px','top':this._getPosition('y')+'px','width':this.options.startWidth+'px','height':this.options.startHeight+'px'});
			if(this.options.draggable == true)
				this.wrapper.draggable({
					start: function(){
						wrapper.css('z-Index',instanceNumber+1);
					},
					stop: function(){
						wrapper.css('z-Index',instanceNumber);
					},
					containment:'document'
				});
			if(this.options.resizable == true){
				this.wrapper
					.resizable({
//						alsoResize: '.atwindow-content, .atwindow-bottom, .atwindow-bottomcontent', // TODO change to objects
						alsoResize: '.mb-draggable-'+instanceNumber,
						maxHeight: 423,
						maxWidth: 803,
						minWidth: 150,
						minHeight: 80
					});
			}
			if(this.options.collapsible == true){
				this.min.bind("click.atwindow", $.proxy(this,"_minimize"));
				this.max.bind("click.atwindow", $.proxy(this,"_maximize"));
			}
			if(this.options.close == true)
				this.close.bind("click.atwindow", $.proxy(this,"_windowClose"));

//			this._isOpen = false;
//			if(this._isOpen == false)
//					wrapper.hide();
	},

	setContent: function(input){
		this.content.text(input);
	},

	setContentAjax: function(actions){
		var content = this.content;
		var url = this.options.url;
//		var splitted = actions.split(',');
//		for(i=0;i<splitted.length;i++){}
		$.ajax({
		  url: url,
		  type: 'post',
		  beforeSend: function(){
			content.html('<table style="width:95%;height:90%;"><tr><td align="center" valign="middle"><img src="images/ajax-loader-1.gif" /></td></tr></table>');
		  },
		  data: actions,
		  success: function(msg){
			content.html(msg);
		  }
		});
	},

	/**
	 * Calculates positioning of the window
	 */
	_getPosition: function(axis){
		var documentWidth = $(document).width();
		var documentHeight = $(document).height();
		var windowWidth = this.options.startWidth;
		var windowHeight = this.options.startHeight;
		var xAdd = this.options.xAdd;
		var yAdd = this.options.yAdd;
		var xPos;
		var yPos;
		switch(axis){
			case'x':
				switch(this.options.xPosition){
					case'center':
						xPos = documentWidth / 2 - windowWidth / 2 + xAdd;
					break;
					case'left':
						xPos = 0 + xAdd;
					break;
					case'right':
						xPos = documentWidth - windowWidth + xAdd;
					break;
				}
				return xPos;
			break;
			case'y':
				switch(this.options.yPosition){
					case'center':
						yPos = documentHeight / 2 - windowHeight / 2 + yAdd;
					break;
					case'top':
						yPos = 0 + yAdd;
					break;
					case'bottom':
						yPos = documentHeight - windowHeight + yAdd;
					break;
				}
				return yPos;
			break;
		}
	},

	_init: function() {
		if (this.options.autoOpen) {
			this.wrapper.show();
		}
	},

	destroy: function() {

	},

	_minimize: function (){
		var oSpeed = this.options.openSpeed;
		if(this.options.resizable == true)
			this.resize.hide();
		this.min.hide();
		this.max.show();

		this._setTempHeight(this.bottom.height());

		this.bottom.animate({
			height: '10px'
		}, oSpeed);
		this.bottomcontent.animate({
			height: '10px'
		}, oSpeed);

		this.content.animate({
			height: 0
		}, oSpeed,function(){
			$(this).hide();
		});
	},

	_maximize: function(){
		var oSpeed = this.options.openSpeed;
		var resizable = this.options.resizable;
		var tempH = this._setTempHeight();
		this.min.show();
		this.content.show();
		this.max.hide();
		this.bottom.animate({
			height: tempH
		}, oSpeed);
		this.bottomcontent.animate({
			height: tempH
		}, oSpeed);
		this.content.animate({
			height: tempH-15
		}, oSpeed,function(){
			if(resizable == true)
				$(this).parent().find('.atwindow-resize').show();
		});
	},

	_setTempHeight: function(e){
		if(e == null)
			return this.tempH;
		else
			this.tempH = e;
	},

	_windowClose: function(){
		var oSpeed = this.options.openSpeed;
		this.wrapper.hide(oSpeed);
	},

	open: function(){
		var oSpeed = this.options.openSpeed;
		this.wrapper.show(oSpeed);
		this._trigger('open');
	}

//	isOpen: function() {
//		return this._isOpen;
//	},

	});
})(jQuery);